n=eval(input())
people=list(range(1,n+1))
i=0
index=0
while len(people)>1:
    i=i+1
    c=i
    if c%7==0 or '7' in str(c):
        people.pop(index)
    else:
        index=index+1
    if people:
        index%=len(people)
print(people[0])